package com.gzx.plugin.fin.tech.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gzx.plugin.fin.tech.dto.GzxBlackRespDto;
import com.gzx.plugin.fin.tech.entity.GzxBlackList;
import com.gzx.plugin.fin.tech.mapper.GzxBlackListMapper;
import com.gzx.plugin.fin.tech.service.GzxBlackListDbService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.util.CommonDateUtil;
import vip.xiaonuo.common.util.CommonSleepUtil;

import java.util.List;
import java.util.stream.Collectors;

import static org.apache.commons.lang.StringEscapeUtils.escapeSql;

/**
 * @author zjt
 * @description
 * @date 2025/5/20 10:05
 */
@Service
@RequiredArgsConstructor
public class GzxBlackListDbServiceImpl implements GzxBlackListDbService {

    private final GzxBlackListMapper blackListMapper;

    /**
     * 查询黑名单
     *
     * @param lambdaQueryWrapper
     * @return
     */
    @Override
    public List<GzxBlackList> selectList(LambdaQueryWrapper<GzxBlackList> lambdaQueryWrapper) {
        return blackListMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 批量保存到数据库
     *
     * @param type
     * @param dataRespList
     */
    @Override
    public void saveBatch(Integer type, List<GzxBlackRespDto.DataResp> dataRespList) {
        String currentTime = CommonDateUtil.getCurrentTimeStr();
        String batchSql = dataRespList.stream()
                .map(s -> String.format("(%s, '%s', %s, '%s')",
                        escapeSql(String.valueOf(type)),
                        escapeSql(s.getCallee()),
                        escapeSql(String.valueOf(s.getForbid())),
                        escapeSql(currentTime)))
                .collect(Collectors.joining(", "));
        blackListMapper.saveDb(batchSql);
        CommonSleepUtil.sleep100();
    }
}
